package com.i2bt.app.share.model;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.UserConfig;

import com.i2bt.app.BaseFragment;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.share.ShareFragment;
import com.i2bt.bean.GetSharedBean;
import com.i2bt.util.FileSizeConvertUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetMyShareJsonHandle extends BaseJsonModel {

	ShareFragment frg;

	public GetMyShareJsonHandle(BaseFragment fragment) {
		super(fragment);
		// TODO Auto-generated constructor stub
		this.frg = (ShareFragment) fragment;
	}

	@Override
	protected String logicToWrap() throws JSONException {
		// TODO Auto-generated method stub
		try {
			jsonParam.put(AndroidRestContract.ACTION,
					AndroidRestContract.GET_SHERE_LIST);
			jsonParam.put(AndroidRestContract.TOKEN, UserConfig.token);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonParam.toString();
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		// TODO Auto-generated method stub
		if (isSuc) {
			try {
				JSONObject ja = new JSONObject(result);
				boolean succ = ja.getBoolean(AndroidRestContract.SUCCEED);
				String mes = null;
				if (succ) {
					if (ja.has(AndroidRestContract.SHARE_INFO)) {
						JSONArray list = ja
								.getJSONArray(AndroidRestContract.SHARE_INFO);
						this.frg.mySharedFilesList.clear();
						if (list != null) {
							for (int i = 0; i < list.length(); i++) {
								JSONObject item = list.getJSONObject(i);
								GetSharedBean bean = new GetSharedBean();

								JSONArray f = item
										.getJSONArray(AndroidRestContract.FRIEND_LIST);
								String friendList[] = new String[f.length()];
								for (int j = 0; j < f.length(); j++) {
									friendList[j] = f.get(j).toString();
								}
								bean.setShareList(friendList);

								bean.setShareDate(item
										.getString(AndroidRestContract.SHARE_DATE));

								bean.setFileSize(item
										.getString(AndroidRestContract.FILE_SIZE));

								bean.setHomeBoxPath(item
										.getString(AndroidRestContract.SHARE_FOLDERS));

								bean.setFileName(item
										.getString(AndroidRestContract.SHARE_HOMEBOX_NAME));

								bean.setFile(this.isFile(bean.getFileSize()));
								bean.setHomebox(this.isHomeBox(
										bean.getHomeBoxPath(), bean.isFile()));
								if (!bean.isHomebox()) {
									bean.setFileName(this.getFileName(bean
											.getHomeBoxPath()));
								}
								bean.setFileSize(FileSizeConvertUtil
										.autoConvert(Integer.parseInt(bean
												.getFileSize())));

								this.frg.mySharedFilesList.add(bean);
							}
						}
					} else {
						this.frg.toastHandler("没有分享给好友");
					}
					this.frg.mySharedListAdapter.notifyDataSetChanged();
					this.frg.mySharedListView.stopRefresh();
				} else {
					this.frg.toastHandler(ja.get(AndroidRestContract.MESSAGE)
							.toString());
					this.frg.mySharedListView.stopRefresh();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.frg.mySharedListView.stopRefresh();
			}
		} else {
			this.frg.toastHandler("网络连接失败");
			this.frg.mySharedListView.stopRefresh();
		}
	}

	public boolean isFile(String size) {
		if (size.equals("0")) {
			return false;
		} else {
			return true;
		}
	}

	public boolean isHomeBox(String path, boolean isFile) {
		if (isFile) {
			return false;
		}
		if (path.indexOf('\\') == -1) {
			return true;
		}
		return false;
	}

	public String getFileName(String path) {
		String str = path;
		int k = 0;
		for (int i = 0; i < str.length(); i++) {
			k = str.indexOf('\\');
			if (k == -1) {
				break;
			}
			str = str.substring(k + 1);
		}
		return str;
	}
}
